package com.keyinong.caizhengting.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.ImageUtils;
import com.keyinong.caizhengting.entity.ShangpinEntity;

import java.util.ArrayList;

/**
 * 首页商品适配器
 */

public class ShangpintuijianAdapter extends BaseAdapter {
    Context context;
    ArrayList<ShangpinEntity> shangpinList;
    HttpTools httpTools = null;

    public ShangpintuijianAdapter(Context context, ArrayList<ShangpinEntity> shangpinList) {
        super();
        this.context = context;
        this.shangpinList = shangpinList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return shangpinList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return shangpinList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHandle handle;
        if (view == null) {
            handle = new ViewHandle();
            view = LayoutInflater.from(context).inflate(R.layout.item_shangpin, null);
            handle.iv_shangpin = (ImageView) view.findViewById(R.id.imageView1);
            handle.tv_shangpinming = (TextView) view.findViewById(R.id.tv_shangpinming);
            handle.tv_price = (TextView) view.findViewById(R.id.tv_price);
            handle.tv_unit = (TextView) view.findViewById(R.id.tv_unit);
            view.setTag(handle);
        } else {
            handle = (ViewHandle) view.getTag();
        }
        handle.tv_shangpinming.setText(shangpinList.get(position).getName());
//		handle.tv_price.setText(""+(int)(Double.parseDouble(shangpinList.get(position).getPrice())*10));
        handle.tv_price.setText("" + shangpinList.get(position).getPrice());
//		handle.tv_unit.setText("/"+shangpinList.get(position).getUnit());
        ImageUtils.displayImage(context,
                (shangpinList.get(position).getImage()).replace("w/60", "w/120"),
                handle.iv_shangpin, R.drawable.wupian1);
        return view;
    }

    class ViewHandle {
        /**
         * 图片
         */
        ImageView iv_shangpin;
        /**
         * 商品名
         */
        TextView tv_shangpinming;
        /**
         * 价格
         */
        TextView tv_price;
        /**
         * 单位
         */
        TextView tv_unit;
    }
}


